<?php
 /******************************************************************************
 *   GunCMS is an avid supporter of open source software. This is the appropriate 
 *   option if you are creating an open source application with a license 
 *   compatible with the GNU GPL license v3.
 *   For details http://www.gnu.org/licenses/gpl.html
 *   DO NOT USE FOR COMMERCIAL WITHOUT PERMISSION
 *
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 ********************************************************************************/
class FgcmsProduct {
	var $db;
 	var $smarty;	
	public function __construct($db, $smarty) {
		//contructor function
		$this->db = $db;
		$this->smarty = $smarty;
	}
	
	/* Assigning the content related to the category_id to the 
	 * Smarty-Variable 'PAGE'. Because using ezSql, PAGE is a pointer!
	 * One should be aware of this fact, when PAGE is used in the 
	 * Smarty-Templates. Output is generated ba: {$PAGE->page_content}.
	 */ 
	public function process() {
		if(isset($_POST['act']) && $_POST['act'] !=""){
			$act = $_POST['act'];
			if($act == 'addtobasket')
				FgcmsProduct::add_basket();
		}
		if((isset($_GET['subcat']) || isset($_GET['cat'])) && !isset($_GET['page'])){
			if(isset($_GET['subcat']) && $_GET['subcat']!=""){
				$link = "?cat=".$_GET['cat']."&subcat=".$_GET['subcat'];
				$catid = $_GET['subcat'];
			}else{
				$link = "?cat=".$_GET['cat'];
				$catid = $_GET['cat'];
			}		
			$count = FgcmsProductBean::count_product_by_cat($this->db,$catid);
			if($count>0){
				$cat = FgcmsCategoryBean::get_catname_by_id($this->db,$catid);
				$getproduct	= FgcmsProductBean::get_product_by_cat_paging($this->db,PRODUCTPAGING,"list",$catid);
				if($getproduct!=""){
					foreach($getproduct as $item){	
						$item->thumb_image = UPLOAD_PRODUCT_THUMB.$item->product_thumbnail;	
						if($item->specials_new_products_price!=""){
							$item->saleoff = number_format(($item->product_price - $item->specials_new_products_price)*100/$item->product_price);
							$item->new_price = $item->specials_new_products_price;
						}else if($item->featured_new_products_price!=""){	
							$item->saleoff = number_format(($item->product_price - $item->featured_new_products_price)*100/$item->product_price);
							$item->new_price = $item->featured_new_products_price;	
						}	
					}
				}
				$product['LIST'] = $getproduct;
				$result = FgcmsPaging::do_paging($count,$product, ADMINPAGING, $link,"list");
				$this->smarty->assign('PAGE_NAME',$cat->cat_name);
				$this->smarty->assign('PRODUCT',$result);
			}
		}else if(isset($_GET['page']) && $_GET['page'] == 'product_detail'){
			if(isset($_GET['subcat']) && $_GET['subcat']!="")
				$catid = $_GET['subcat'];
			else
				$catid = $_GET['cat'];
			$id = $_GET['id'];
			$product = FgcmsProductBean::getProductbyId($this->db,$id);	
			$count = FgcmsProductBean::get_product_by_cat($this->db,$catid);
			if($product->specials_new_products_price!=""){
				$product->saleoff = number_format(($product->product_price - $product->specials_new_products_price)*100/$product->product_price);
				$product->new_price = $product->specials_new_products_price;
			}else if($product->featured_new_products_price!=""){	
				$product->saleoff = number_format(($product->product_price - $product->featured_new_products_price)*100/$product->product_price);
				$product->new_price = $product->featured_new_products_price;	
			}	
		
			
			$dem = 1;
			$check="";
			$pre ="";
			$next = "";
			foreach($count as $item){
				if($check ==1){
					$next = $item->product_id;
					break;
				}else{
					if($item->product_id == $id)
						$check = 1;
					else{
						$dem++;
						$pre =$item->product_id;
					}
				}		
			}
			
			if(isset($_POST['act']) && $_POST['act'] =="recomment"){
				$host = $_SERVER['HTTP_HOST'].FOLDER_HOST;
				$from = FROMMAIL;
				$to = $_POST['email'];
				$fromname = "T & L Tec GdbR";
				$subject = "GunWebshop Checkout Order";
				//$str = $this->smarty->fetch("checkout/email_order.tpl");
				$message = 	$product->product_name;
				$header = "Content-type: text/html\r\nFrom: $from\r\nReply-to: $to";
				FgcmsEmail::send_mail($to, $message, $subject, $from, $fromname, false);
				$type = array('ID' => -1, 'message' => "Send email to friend successful");
				echo array2json($type);	
			}
			$photos=FgcmsProductBean::PhotosByProductId($this->db,$id);
			$product->thumb_image = $product->product_thumbnail;	
			$product->photos =$photos;
			$product->total_product = count($count);
			$product->position_product = $dem;
			$product->product_next = $next;
			$product->product_pre = $pre;
			$this->smarty->assign('PATH',UPLOAD_PRODUCT);
			$this->smarty->assign('PATHTHUMB',UPLOAD_PRODUCT_THUMB);
			$this->smarty->assign('PRODUCT_DETAIL',$product);
		}else if(isset($_GET['page']) && $_GET['page'] == 'product_search'){
			if(isset($_GET['keyproduct']) && $_GET['keyproduct']!=""){
				$count = FgcmsProductBean::count_product_by_name($this->db,$_GET['keyproduct']);
				$productsearch = 	FgcmsProductBean::get_product_by_name_paging($this->db,PRODUCTPAGING,"list",$_GET['keyproduct']);
				if($productsearch!=""){
					foreach($productsearch as $item){	
						$item->thumb_image = UPLOAD_PRODUCT_THUMB.$item->product_thumbnail;	
					}
				}	
				$product['LIST'] = $productsearch;
				$result = FgcmsPaging::do_paging($count,$product, ADMINPAGING, $link,"list");
				$this->smarty->assign('PAGE_NAME',"LABEL_SEARCH_PRODUCTS");
				$this->smarty->assign('PRODUCTSEARCH',$result);
			}else{
				if(isset($_POST['productsearch']) && $_POST['productsearch']!="")
					header("Location: ./?page=product_search&keyproduct=".$_POST['productsearch']);
			}						
		}
	}
	
	public function view_all_specials() {
		$special = FgcmsProductBean::getAllSpecial_paging($this->db,PRODUCTPAGING,"list");	
		$countspecial = FgcmsProductBean::countallSpecial($this->db);
		if($special!=""){
			foreach($special as $item){	
				$item->thumb_image = UPLOAD_PRODUCT_THUMB.$item->product_thumbnail;	
			}
		}	
		$product['LIST'] = $special;
		$result = FgcmsPaging::do_paging($countspecial,$product, ADMINPAGING, $link,"list");
		$this->smarty->assign('PAGE_NAME',"LABEL_ALL_SPECIAL_PRODUCTS");
		$this->smarty->assign('PRODUCT',$result);
	}
	
	public function view_all_featured() {
		$featured = FgcmsProductBean::getAllFeatured_paging($this->db,PRODUCTPAGING,"list");	
		$countfeatured = FgcmsProductBean::countallFeatured($this->db);
		if($featured!=""){
			foreach($featured as $item){	
				$item->thumb_image = UPLOAD_PRODUCT_THUMB.$item->product_thumbnail;	
			}
		}	
		$product['LIST'] = $featured;
		$result = FgcmsPaging::do_paging($countfeatured,$product, ADMINPAGING, $link,"list");
		$this->smarty->assign('PAGE_NAME',"LABEL_ALL_FEATURED_PRODUCTS");
		$this->smarty->assign('PRODUCT',$result);
	}
	
	public function view_product_right() {
		$special = FgcmsProductBean::getSpecialLimit($this->db);
		$countspecial = FgcmsProductBean::countallSpecial($this->db);
		
		if($special!=""){
			if($countspecial > count($special))
			$this->smarty->assign('MORESPECIALS','1');
			foreach($special as $item){	
				$item->thumb_image = UPLOAD_PRODUCT_THUMB.$item->product_thumbnail;	
				$item->saleoff = number_format(($item->product_price - $item->specials_new_products_price)*100/$item->product_price);	
			}
			$this->smarty->assign('SPECIALS',$special);
		}
		
		$featured = FgcmsProductBean::getFeaturedLimit($this->db);
		$countfeatured = FgcmsProductBean::countallFeatured($this->db);
		
		if($featured!=""){
			if($countfeatured > count($featured))
				$this->smarty->assign('MOREFEATURED','1');
			foreach($featured as $item){	
				$item->thumb_image = UPLOAD_PRODUCT_THUMB.$item->product_thumbnail;	
				$item->saleoff = number_format(($item->product_price - $item->featured_new_products_price)*100/$item->product_price);	
			}
			$this->smarty->assign('FEATURED',$featured);
		}
		
	}
	
	public function add_basket() {
		if(isset($_POST['product_id']) && $_POST['quantity'] != 0 && ctype_digit($_POST['quantity']))
		{
			if(!isset($_SESSION['total']))
				{
					$_SESSION['total'] = 0;
				}
				$temp = 0;
				if($_SESSION['total'] >= 1)
				{
					for($i=1;$i<=$_SESSION['total'];$i++)
					{
						$arrproduct = split("[,]",$_SESSION['pro'.$i]);
						if($_POST['product_id'] == $arrproduct[0])
						{
							$temp = 1;
							$_SESSION['pro'.$i] = $_POST['product_id'].",".($_POST['quantity'] + $arrproduct[1]);
						}
					}
				}
				if($temp == 0)
				{
					$k = $_SESSION['total'] + 1;
					$_SESSION['pro'.$k] = $_POST['product_id'].",".$_POST['quantity'];
					$_SESSION['total']++;
				}
				header("Location: ./?page=checkout_basket");
		}
	}
}
?>
